<template>
  <div>
    <el-card>
      <el-form :inline="true" size="normal">
        <el-form-item>
          <el-button type="primary" @click="openForm" size="small">新增</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="dataList" border stripe>
        <el-table-column
          label="序号"
          type="index"
          width="80px"
        ></el-table-column>
        <el-table-column label="id" prop="id"></el-table-column>
        <el-table-column label="账号" prop="username"></el-table-column>
        <el-table-column label="名称" prop="name"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div>
              <el-button type="primary" size="small" @click="openEditor(scope.row)">编辑</el-button>
              <el-button type="danger" size="small" @click="del(scope.row.id)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog :title="formTitle" :visible.sync="dialogVisible" width="30%">
      <div>
        <el-form :model="form" ref="form"  label-width="60px">
          <el-form-item label="账号">
            <el-input v-model="form.username" placeholder="请输入账号"></el-input>
          </el-form-item>
          <el-form-item label="名称">
            <el-input v-model="form.name" placeholder="请输入名称"></el-input>
          </el-form-item>
          <el-form-item label="密码">
            <el-input v-model="form.password" type="password" placeholder="请输入密码"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submit">提交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getList,create,update,remove } from "@/api/editor/index";
export default {
  data() {
    return {
      id:'',
      dataList: [],
      query: {
        page: 1,
        pageSize: 50 //估计没多少 就不处理分页了
      },
      total: 0,
      dialogVisible: false,
      formTitle: "新增字幕编辑",
      form:{
        username:'',
        name:'',
        password:'',
      }
    };
  },
  created() {
    this.getDataList();
  },
  methods: {
    getDataList() {
      getList(this.query).then(res => {
        this.dataList = res.data.list;
        this.total = res.data.total;
      });
    },
    openForm() {
      this.dialogVisible = true;
      this.id = ''
    },
    submit(){
      if(this.id){
        let id = this.id
        update({id,...this.form}).then(res=>{
          this.getDataList()
          this.dialogVisible = false
        })
      }else{
        create(this.form).then(res=>{
          this.getDataList()
          this.dialogVisible = false
        })
      }
    },
    openEditor(row){
      this.dialogVisible = true
      this.id = row.id
      this.form = row
    },
    del(id){
      remove(id).then(res=>{
        this.getDataList()
      })
    }
  }
};
</script>

<style scoped></style>
